﻿/*
' Copyright (c) 2014  Christoc.com
'  All rights reserved.
' 
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
' DEALINGS IN THE SOFTWARE.
' 
*/

using DotNetNuke.Entities.Users;
using System;
using System.Web.UI.WebControls;
using CorpGroup.Modules.cgDocuments.Components;
using DotNetNuke.Security;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Services.Localization;
using DotNetNuke.UI.Utilities;

using System.Collections.Generic;
using Telerik.Web.UI.Widgets;
using Telerik.Web.UI.FileExplorer;
using Telerik.Web.UI;
using System.Web;
namespace CorpGroup.Modules.cgDocuments
{
    /// -----------------------------------------------------------------------------
    /// <summary>   
    /// The Edit class is used to manage content
    /// 
    /// Typically your edit control would be used to create new content, or edit existing content within your module.
    /// The ControlKey for this control is "Edit", and is defined in the manifest (.dnn) file.
    /// 
    /// Because the control inherits from DocumentsModuleBase you have access to any custom properties
    /// defined there, as well as properties from DNN such as PortalId, ModuleId, TabId, UserId and many more.
    /// 
    /// </summary>
    /// -----------------------------------------------------------------------------
    public partial class Edit : cgDocumentsModuleBase
    {
        protected string PortalPath
        {
            get
            {
                return "~/Portals/" + PortalId.ToString();
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                // This code sets RadFileExplorer's paths
                string[] paths = new string[] { PortalPath };
                //Valid Files to view
                //string[] ValidFiles = new string[] { "*.jpg", "*.jpeg", "*.gif", "*.png" };
                string[] ValidFiles = new string[] { "*.pdf"};
                

                RadFileExplorer1.ExplorerMode = FileExplorerMode.FileTree;
                RadFileExplorer1.EnableFilterTextBox = true;
                RadFileExplorer1.FilterTextBoxLabel = "File Name";
                
                RadFileExplorer1.Configuration.ViewPaths = paths;
                RadFileExplorer1.Configuration.UploadPaths = paths;
                RadFileExplorer1.Configuration.DeletePaths = paths;
                RadFileExplorer1.Configuration.SearchPatterns = ValidFiles;

                // Sets Max file size
                RadFileExplorer1.Configuration.MaxUploadFileSize = 10485760;

                // Enables all controls
                RadFileExplorer1.VisibleControls = FileExplorerControls.All;

                //Enable copy
                RadFileExplorer1.EnableCopy = true;

                RadFileExplorer1.EnableCreateNewFolder = true;
                RadFileExplorer1.Configuration.EnableAsyncUpload = true;
                RadFileExplorer1.Enabled = true;

                //Java script to run when selected
                RadFileExplorer1.OnClientItemSelected = "OnClientItemSelected";

                //File explorer mode
                RadFileExplorer1.ExplorerMode = FileExplorerMode.Thumbnails;
                
                // Sets the page size
                RadFileExplorer1.PageSize = 20;

                //Load the default FileSystemContentProvider
                RadFileExplorer1.Configuration.ContentProviderTypeName = typeof(Telerik.Web.UI.Widgets.FileSystemContentProvider).AssemblyQualifiedName;

                //// Enables Paging on the Grid
                //RadFileExplorer1.AllowPaging = true;
   
             //Implement your edit logic for your module
                if (!Page.IsPostBack)
                {
                    //check if we have an ID passed in via a querystring parameter, if so, load that item to edit.
                    //ItemId is defined in the ItemModuleBase.cs file
                    if (DocId > 0)
                    {
                        var dc = new DocumentController();
                        var d = dc.GetDocument(DocId, ModuleId);
                        if (d != null)
                        {
                            txtDocTitle.Text = d.DocTitle;
                            txtDocDescription.Text = d.DocDescription;
                            txtDocPath.Text = d.DocPath;
                        }
                    }
                }
            }
            catch (Exception exc)
            {
                
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            var d = new Document();
            var dc = new DocumentController();

            if (DocId > 0)
            {
                d = dc.GetDocument(DocId, ModuleId);
                d.DocTitle = txtDocTitle.Text.Trim();
                d.DocDescription = txtDocDescription.Text.Trim();
                d.DocPath = txtDocPath.Text.Trim();
                
            }
            else
            {
                d = new Document()
                {
                    CreatedByUserId = UserId,
                    CreatedOnDate = DateTime.Now,
                    DocTitle = txtDocTitle.Text.Trim(),
                    DocDescription = txtDocDescription.Text.Trim(),
                    DocSortOrderId = dc.GetNextSortid(ModuleId),
                    DocPath = txtDocPath.Text.Trim()
                };
            }

            d.LastModifiedByUserId = UserId;
            d.LastModifiedOnDate = DateTime.Now;
            d.ModuleId = ModuleId;

            if (d.DocId > 0)
            {
                dc.UpdateDocument(d);
            }
            else
            {
                dc.CreateDocument(d);
            }
            Response.Redirect(DotNetNuke.Common.Globals.NavigateURL());
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect(DotNetNuke.Common.Globals.NavigateURL());
        }

        protected void btnSelectFile_Click(object sender, EventArgs e)
        {
            pnlFileSelect.Visible = false;
            pnlMainForm.Visible = true;
            //txtDocPath.Text = selectedFile.ToString();
            txtDocPath.Text = selectedFile.Value.ToString();
        }

        protected void btnCancelFile_Click(object sender, EventArgs e)
        {
            pnlFileSelect.Visible = false;
            pnlMainForm.Visible = true;
            
        }

        protected void btnSelectDocument_Click(object sender, EventArgs e)
        {
            pnlFileSelect.Visible = true;
            pnlMainForm.Visible = false;
            selectedFile.Value = txtDocPath.Text;
        }
    }
}